<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>常用参数管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				},
				rules:{
					name:{
						remote: {
						    url: $("#ctx").val()+"/ins/insField/getByName",     //后台处理程序
						    type: "post",               //数据发送方式
						    dataType: "json",           //接受数据格式   
						    data: {                     //要传递的数据
						        name: function() {
						            return $("#name").val();
						        },
						        id: function(){
						        	return $("#id").val();
						        }
						    }
						}
					},
					code:{
						remote: {
						    url: $("#ctx").val()+"/ins/insField/getByCode",     //后台处理程序
						    type: "post",               //数据发送方式
						    dataType: "json",           //接受数据格式   
						    data: {                     //要传递的数据
						        code: function() {
						            return $("#code").val();
						        },
						        id: function(){
						        	return $("#id").val();
						        }
						    }
						}
					}
				},
				messages:{
					name:{
						remote:"该常用参数名称已存在！"
					},
					code:{
						remote:"该占位符已存在!"
					}
				}
			});
			//给录入方式绑定点击事件
			$(".preInput").live("click",function(){
				var val = $(this).val();
				var textarea = $("#preValues");
				if(val == "0"){
					textarea.attr("disabled","disabled");
				}else if(val == "1"||val == "2"){
					textarea.removeAttr("disabled");
				}
			});
			//初始化点击录入方式
			var preInputValue = $("#preInputValue").val();
			$(".preInput[value="+preInputValue+"]").trigger("click");
			//控制数值限制按钮是否可用
			if($("#type").val()=='2'){
				$("#numberButton").removeAttr("disabled");
			}
			$("#type").change(function(){
				var val = $(this).val();
				if(val == '2'){
					$("#numberButton").removeAttr("disabled");
				}else{
					$("#numberButton").attr("disabled","disabled");
				}
			});
			//数值范围按钮事件
			$("#numberButton").click(function(){
				var btn = $(this);
				var numberFrom = $("#numberFrom").val();
				var numberTo = $("#numberTo").val();
				var enableNumberRange = $("#enableNumberRange").val();
				var digit = $("#digit").val();
				var url = "${ctx}/ins/insTable/numberRange?numberFrom="+numberFrom+"&numberTo="+numberTo+"&enableNumberRange="+enableNumberRange+"&digit="+digit;
				top.$.jBox.open("iframe:"+url, "数值范围",810,$(top.document).height()-120,{
	                buttons:{"提交":"ok", "关闭":true}, bottomText:"数值范围",submit:function(v, h, f){
	                    var code;
	                    if (v=="ok"){
	                    	// 执行保存
	                        //loading('正在提交，请稍等...');
	                    	var flag = h.find("iframe")[0].contentWindow.submitForm();
	                    	if(flag){
	                    		var numberFrom = h.find("iframe")[0].contentWindow.$("#numberFrom").val();
								var numberTo = h.find("iframe")[0].contentWindow.$("#numberTo").val();
								var enableNumberRange=h.find("iframe")[0].contentWindow.$("#enableNumberRange").prop("checked");
								var digit = h.find("iframe")[0].contentWindow.$("#digit").val();
								$("#numberFrom").val(numberFrom);
								$("#numberTo").val(numberTo);
								$("#enableNumberRange").val(enableNumberRange?"1":"0");
								$("#digit").val(digit);
								top.$.jBox.close(true);
	                    	}
	                        return false;
	                    }
	                }, loaded:function(h){
	                    $(".jbox-content", top.document).css("overflow-y","hidden");
	                }, closed:function(){
	                }
	            });
			});
			//索引列默认选中逻辑
			if($("#indexedValue").val()=="1"){
				$("#indexed").prop("checked",true);
				//唯一和必填不可选
				$(".require").attr("disabled","disabled");
				$(".uniquely").attr("disabled","disabled");
				//唯一选中
				$("#uniquely0").removeAttr("checked");
				$("#uniquely1").attr("checked","checked");
				//必填选中
				$("#required0").removeAttr("checked");
				$("#required1").attr("checked","checked");
			}
			//索引列点击事件
			$("#indexed").click(function(){
				//选中了
				if($(this).prop("checked")){
					$("#indexedValue").val("1");
					//唯一和必填不可选
					$(".require").attr("disabled","disabled");
					$(".uniquely").attr("disabled","disabled");
					//唯一选中
					$("#uniquely0").removeAttr("checked");
					$("#uniquely1").attr("checked","checked");
					//必填选中
					$("#required0").removeAttr("checked");
					$("#required1").attr("checked","checked");
				}else{
					$("#indexedValue").val("0");
					//唯一和必填可选
					$(".require").removeAttr("disabled");
					$(".uniquely").removeAttr("disabled");
				}
			});
		});
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/ins/insField/">常用参数列表</a></li>
		<li class="active"><a href="${ctx}/ins/insField/form?id=${insField.id}">常用参数<shiro:hasPermission name="ins:insField:edit">${not empty insField.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="ins:insField:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="insField" action="${ctx}/ins/insField/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input  type="hidden"  id="ctx" value="${ctx}"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">常用参数名称：</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">占位符：</label>
			<div class="controls">
				<form:input path="code" htmlEscape="false" maxlength="64" class="input-xlarge"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">默认值：</label>
			<div class="controls">
				<form:input path="defaultValue" htmlEscape="false" maxlength="64" class="input-xlarge"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">数据类型：</label>
			<div class="controls">
				<input type="hidden" id="numberFrom" name="numberFrom" value="${insField.numberFrom}"/>
				<input type="hidden" id="numberTo" name="numberTo" value="${insField.numberTo}"/>
				<input type="hidden" id="enableNumberRange" name="enableNumberRange" value="${insField.enableNumberRange}"/>
				<input type="hidden" id="digit" name="digit" value="${insField.digit}"/>
				<select id="type" name="type" data-value="${insField.type}" class="input-small required">
					<option value="1" <c:if test="${insField.type=='1'}">selected</c:if>>字符串</option>
					<option value="2" <c:if test="${insField.type=='2'}">selected</c:if>>数字</option>
					<option value="3" <c:if test="${insField.type=='3'}">selected</c:if>>年月日时分秒</option>
					<option value="4" <c:if test="${insField.type=='4'}">selected</c:if>>年月日</option>
					<option value="5" <c:if test="${insField.type=='5'}">selected</c:if>>年月</option>
				</select>
				<input id="numberButton" type="button" value="数值限制" class="numberButton btn" disabled/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">预录入：</label>
			<div class="controls">
				<input type="hidden" id="preInputValue" value="${insField.preInput}"/>
				<span><input class="preInput" id="preInput0" name="preInput" type="radio" value="0"><label for="preInput0">录入</label></span>
				<span><input class="preInput" id="preInput1" name="preInput" type="radio" value="1"><label for="preInput1">选择</label></span>
				<span><input class="preInput" id="preInput2" name="preInput" type="radio" value="2"><label for="preInput1">计算</label></span>
				<span><textarea disabled id="preValues" name="preValues" rows="1" maxlength="1000" class="input-small ">${insField.preValues}</textarea></span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">必填：</label>
			<div class="controls">
				<span><input <c:if test="${insField.required==0}">checked</c:if> class="require" id="required0" name="required" type="radio" value="0"><label for="required0">非必填</label></span>
				<span><input <c:if test="${insField.required==1}">checked</c:if> class="require" id="required1" name="required" type="radio" value="1"><label for="required1">必填</label></span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">重复：</label>
			<div class="controls">
				<span><input <c:if test="${insField.uniquely==0}">checked</c:if> class="uniquely" id="uniquely0" name="uniquely" type="radio" value="0"><label for="uniquely0">可重复</label></span>
				<span><input <c:if test="${insField.uniquely==1}">checked</c:if> class="uniquely" id="uniquely1" name="uniquely" type="radio" value="1"><label for="uniquely1">唯一</label></span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">索引列：</label>
			<div class="controls">
				<input type="hidden" value="${insField.indexed}" name="indexed" id="indexedValue"/>
				<span><input id="indexed" type="checkbox" value="1"></span>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="ins:insField:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>